package io.hepu.robotize.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class JBase64 {
    static final Charset CHARSET =  StandardCharsets.UTF_8;

    private JBase64() {}

    /**
     * 加密
     * @since JDK1.8
     */
    public static String encode(String plain) {
        if (plain == null || plain.isEmpty()) {
            throw new IllegalArgumentException("Encode input cannot be null or empty");
        }
        byte[] encodeBytes = Base64.getEncoder().encode(plain.getBytes(CHARSET));
        return new String(encodeBytes, CHARSET);
    }

    /**
     * 解密
     * @since JDK1.8
     */
    public static String decode(String cipher) {
        if (cipher == null || cipher.isEmpty()) {
            throw new IllegalArgumentException("Decode input cannot be null or empty");
        }
        byte[] decodeBytes = Base64.getDecoder().decode(cipher.getBytes(CHARSET));
        return new String(decodeBytes, CHARSET);
    }
}
